using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using OpenComposite.Repository.Properties;
using System.Drawing;
using OpenComposite.EII.Repository;
using OpenComposite.EII.Designer.Workflow;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace OpenComposite.EII.Designer.SCA
{
	public class UIComponentSymbol : ServiceComponentSymbol
	{
		#region Initialization
		public UIComponentSymbol()
		{
			init();
		}
		private void init()
		{
			this.BeginUpdate();

			_img = (Bitmap)Resources.png_form;

			this.BackBrushMode = BackgroundBrushMode.LinearGradient;
			this.BackLinearGradientMode = System.Drawing.Drawing2D.LinearGradientMode.Vertical;
			this.BackColor = Color.FromArgb(121, 137, 251);
			this.BackColor2 = Color.White;

			this.ContextMenuStrip.Items.Insert(0, new ToolStripMenuItem("Open Project...", null, new EventHandler(OpenProject_Click)));
			this.ContextMenuStrip.Items.Insert(1, new ToolStripMenuItem("Select Project...", null, new EventHandler(SelectProject_Click)));
			this.ContextMenuStrip.Items.Insert(2, new ToolStripSeparator());

			this.EndUpdate();
		}
		#endregion

		#region Public Members

		#region Properties
		#endregion

		#region Methods
		public override void PaintBackground(PaintEventArgs pe)
		{
			base.PaintBackground(pe);

			Rectangle rc = this.ClientRectangle;
			Point pt = this.PointToDesignerView(new Point(rc.X, rc.Bottom - 16));
			pe.Graphics.DrawImageUnscaled(_img, pt.X, pt.Y);
		}
		public override void Dispose()
		{
			dispose(_img);
			base.Dispose();
		}
		#endregion

		#endregion

		#region Protected Members

		#region Variables
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private Bitmap _img;
		#endregion

		#region Methods
		private UIComponentProperties getProperties()
		{
			UIComponentProperties props = null;
			Service svc = this.RepositoryItem as Service;
			if ( svc != null ) {
				props = svc.ExtendedProperties.UIComponentProperties;
			}
			return props;
		}
		private void setProperties(UIComponentProperties props)
		{
			Service svc = this.RepositoryItem as Service;
			if ( svc != null && props != null &&
				 svc.ExtendedProperties.UIComponentProperties != props ) {
				svc.ExtendedProperties.UIComponentProperties = props;
			}
		}
		private void openProject()
		{
			UIComponentProperties uiprops = getProperties();
			if ( uiprops == null || string.IsNullOrEmpty(uiprops.ProjectPath) ) {
				if ( !selectProject() )
					return;
			}
			//System.Diagnostics.Process p = new System.Diagnostics.Process();
			//p.StartInfo.FileName = uiprops.ProjectPath;
			//p.StartInfo.WorkingDirectory = Path.GetDirectoryName(uiprops.ProjectPath);
			System.Diagnostics.Process.Start(uiprops.ProjectPath);
		}
		private bool selectProject()
		{
			UIComponentProperties uiprops = getProperties();
			if ( uiprops == null )
				uiprops = new UIComponentProperties();

			OpenFileDialog dlg = new OpenFileDialog();
			dlg.CheckFileExists = true;
			dlg.Filter =
				"All Project Files (*.sln;*.csproj;*.vbproj;*.vbp;*.exe)|*.sln;*.csproj;*.vbproj;*.vbp;*.exe|" +
				"Solution Files (*.sln)|*.sln|" +
				"C# Project Files (*.csproj)|*.csproj|" +
				"VB Project Files (*.vbproj;*.vbp)|*.vbproj;*.vbp|" +
				"Executable Files (*.exe)|*.exe|" +
				"All files (*.*)|*.*";
			dlg.FilterIndex = 1;
			if ( string.IsNullOrEmpty(uiprops.ProjectPath) )
				dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
			else {
				dlg.InitialDirectory = Path.GetDirectoryName(uiprops.ProjectPath);
				dlg.FileName = uiprops.ProjectPath;
			}
			dlg.Multiselect = false;
			dlg.Title = "Select UI Component Project";
			dlg.DefaultExt = "csproj";
			if ( dlg.ShowDialog() == DialogResult.OK ) {
				uiprops.ProjectPath = dlg.FileName;
				setProperties(uiprops);
				return true;
			} else {
				return false;
			}
		}
		#endregion

		#region Event Handlers
		private void OpenProject_Click(object sender, EventArgs e)
		{
			openProject();
		}
		private void SelectProject_Click(object sender, EventArgs e)
		{
			selectProject();
		}
		#endregion

		#endregion
	}
}
